<template>
  <div class="login-mobile">
    <div class="login-container">
      <div class="login-card">
        <div class="logo-section">
          <img src="/fylog1d.png" alt="Logo" class="logo" />
          <h1 class="app-title">退费审核系统</h1>
        </div>
        
        <div class="login-form">
        <div class="form-group">
          <input
            v-model="loginForm.username"
            type="text"
            placeholder="请输入用户名"
            class="form-input"
            :disabled="loading"
          />
        </div>
        
        <div class="form-group">
          <input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            class="form-input"
            :disabled="loading"
            @keyup.enter="handleLogin"
          />
        </div>
        
        <button
          @click="handleLogin"
          :disabled="loading || !loginForm.username || !loginForm.password"
          class="login-btn"
        >
          <span v-if="loading">登录中...</span>
          <span v-else>登录</span>
        </button>
        
        <!-- 登录方式分隔线 -->
        <div v-if="isWeChatWorkEnv" class="login-divider">
          <span>或</span>
        </div>
        
        <!-- 企业微信登录按钮 -->
        <button
          v-if="isWeChatWorkEnv"
          @click="handleWeChatLogin"
          :disabled="wechatLoading"
          class="wechat-login-btn"
        >
          <span v-if="wechatLoading">企业微信登录中...</span>
          <span v-else>企业微信登录</span>
        </button>
      </div>
      </div>
      
      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { isWeChatWork, wechatLogin } from '@/utils/wechat'

const router = useRouter()
const authStore = useAuthStore()

const loading = ref(false)
const wechatLoading = ref(false)
const errorMessage = ref('')
const isWeChatWorkEnv = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

// 检测企业微信环境
onMounted(() => {
  isWeChatWorkEnv.value = isWeChatWork()
  
  // 不自动触发企业微信登录，让用户手动选择登录方式
  // 这样避免在用户输入用户名密码时页面重新加载
})

const handleLogin = async () => {
  if (!loginForm.username || !loginForm.password) {
    errorMessage.value = '请输入用户名和密码'
    return
  }
  
  try {
    loading.value = true
    errorMessage.value = ''
    
    const result = await authStore.login({
      username: loginForm.username,
      password: loginForm.password
    })
    
    if (result.success) {
      // 登录成功，跳转到主页
      router.push('/')
    } else {
      errorMessage.value = result.message || '登录失败，请检查用户名和密码'
    }
    
  } catch (error) {
    console.error('登录失败:', error)
    errorMessage.value = error.message || '登录失败，请检查用户名和密码'
  } finally {
    loading.value = false
  }
}

// 企业微信登录处理
const handleWeChatLogin = async () => {
  try {
    wechatLoading.value = true
    errorMessage.value = ''
    
    const result = await wechatLogin()
    
    if (result.success) {
      // 企业微信登录成功，跳转到主页
      router.push('/')
    } else {
      errorMessage.value = result.message || '企业微信登录失败'
    }
    
  } catch (error) {
    console.error('企业微信登录失败:', error)
    errorMessage.value = error.message || '企业微信登录失败，请重试'
  } finally {
    wechatLoading.value = false
  }
}
</script>

<style scoped>
.login-mobile {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 400px;
}

.login-card {
  background: white;
  border-radius: 16px;
  padding: 40px 30px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.logo-section {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 16px;
}

.app-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.login-form {
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-input {
  width: 100%;
  height: 50px;
  padding: 0 16px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  font-size: 16px;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
}

.form-input:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
}

.login-btn {
  width: 100%;
  height: 50px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
}

.login-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.wechat-login-btn {
  width: 100%;
  height: 50px;
  background: linear-gradient(135deg, #07c160 0%, #00ae56 100%);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  margin-top: 12px;
}

.wechat-login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(7, 193, 96, 0.4);
}

.wechat-login-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.login-divider {
  text-align: center;
  margin: 20px 0;
  position: relative;
}

.login-divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #e1e5e9;
}

.login-divider span {
  background: white;
  padding: 0 16px;
  color: #666;
  font-size: 14px;
}

.error-message {
  background-color: #fee;
  color: #c53030;
  padding: 12px;
  border-radius: 8px;
  text-align: center;
  font-size: 14px;
  border: 1px solid #fed7d7;
}

/* 移动端适配 */
@media (max-width: 480px) {
  .login-mobile {
    padding: 10px;
  }
  
  .login-card {
    padding: 30px 20px;
  }
  
  .app-title {
    font-size: 20px;
  }
  
  .form-input,
  .login-btn {
    height: 45px;
    font-size: 15px;
  }
}
</style>